using System.Collections.Generic;
using UnityEngine;
using FairyGUI;

public class BiologyMO{
    static List<Biology> _biologyDataList = null;//所有生物数据

    private static void InitData()
    {
        if (_biologyDataList != null) return;
        //读取生物数据
        _biologyDataList = Utils.LoadByJson<List<Biology>>(Application.dataPath, "biologyList.json");
        if (_biologyDataList == null) _biologyDataList = new List<Biology>();
    }
    /// <summary>
    /// 获取所有生物数据
    /// </summary>
    /// <returns></returns>
    public static List<Biology> GetBiologyList(){
        InitData();
        return _biologyDataList;
    }
    /// <summary>
    /// 保存生物列表
    /// </summary>
    public static void SaveBiologyDataList(){
        if(_biologyDataList == null) return;
        Utils.SaveByJson<List<Biology>>(Application.dataPath, "biologyList.json",_biologyDataList);
    }
    /// <summary>
    /// 保存生物
    /// </summary>
    /// <param name="biologyData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveBiology(Biology biologyData,int index)
    {
        //新增生物的保存
        if(index == _biologyDataList.Count) {
            _biologyDataList.Add(biologyData);
        }
        else{
            if (index < 0 || index >= _biologyDataList.Count) return false;
            _biologyDataList[index] = biologyData;
        }
        SaveBiologyDataList();
        return true;
    }
    /// <summary>
    /// 删除生物
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelBiology(int index){
        if (index < 0 || index >= _biologyDataList.Count) return false;
        LTLocalization.DelKey(_biologyDataList[index].name);
        LTLocalization.DelKey(_biologyDataList[index].descript);
        foreach(var item in _biologyDataList[index].dialogueList){
            LTLocalization.DelKey(item.dialogueKey);
        }
        _biologyDataList.RemoveAt(index);
        //ResetBiologyId();
        SaveBiologyDataList();
        LTLocalization.SaveDataToCSV();
        return true;
    }
    /// <summary>
    /// 重置生物id
    /// </summary>
    public static void ResetBiologyId(){
        if(_biologyDataList == null) return;
        for(int i = 0;i < _biologyDataList.Count;i++){
            _biologyDataList[i].id = "Biology_"+(i + 1);
            ResetBiologyName(_biologyDataList[i]);
            ResetBiologyDescript(_biologyDataList[i]);
        }
    }
    /// <summary>
    /// 重置生物名字键值
    /// </summary>
    /// <param name="biology"></param>
    public static void ResetBiologyName(Biology biology)
    {
        var newName = biology.id + "_name";
        if(newName != biology.name)
        {
            var isContain = LTLocalization.IsContain(biology.name);
            if (isContain)
            {
                LTLocalization.ReplaceKey(newName,biology.name);
                LTLocalization.DelKey(biology.name);
            }
            biology.name = newName;
        }
    }
    /// <summary>
    /// 重置生物描述键值
    /// </summary>
    /// <param name="biology"></param>
    public static void ResetBiologyDescript(Biology biology)
    {
        var newName = biology.id + "_descript";
        if(newName != biology.descript)
        {
            var isContain = LTLocalization.IsContain(biology.descript);
            if (isContain)
            {
                LTLocalization.ReplaceKey(newName,biology.descript);
                LTLocalization.DelKey(biology.descript);
            }
            biology.descript = newName;
        }
    }
    public static int GetBiologyIndex(Biology biology){
        InitData();
        return _biologyDataList.IndexOf(biology);
    }
    /// <summary>
    /// 获取指定id的数据
    /// </summary>
    /// <param name="biologyId"></param>
    /// <returns></returns>
    public static Biology GetBiologyById(string biologyId){
        InitData();
        foreach(var item in _biologyDataList){
            if(item.id == biologyId) return item;
        }
        return null;
    }
    public static void RemoveAllPointWeapon(string weaponId){
        InitData();
        foreach(var item in _biologyDataList){
            var values = item.GetEquipmentEnumString();
            for(int i = 0;i < values.Length;i++){
                item.RemoveWeapon(i,weaponId);
            }
        }
        SaveBiologyDataList();
    }
    public static void RemoveAllPointCard(string cardId){
        InitData();
        foreach(var item in _biologyDataList){
           var values = item.GetEquipmentEnumString();
            for(int i = 0;i < values.Length;i++){
                item.RemoveCard(i,cardId);
            }
        }
        SaveBiologyDataList();
    }
    public static List<Biology> GetListOfType(GMConfig.BiologyType type)
    {
        InitData();
        List<Biology> dataList = new List<Biology>();
        foreach (var item in _biologyDataList)
        {
            if (item.type == type){
                dataList.Add(item);
            }
        }
        return dataList;
    }
}
